package org.mseco.doc.expensetrack;

import java.util.List;

import org.mseco.doc.expensetrack.dao.TransactionType;
import org.mseco.doc.expensetrack.dao.TransactionTypeDataSource;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class ManageTransactionTypes extends Activity {

	private TransactionTypeDataSource transactionTypeDataSource;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manage_transaction_types);

//		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	    // If your minSdkVersion is 11 or higher, instead use:
	    getActionBar().setDisplayHomeAsUpEnabled(true);

		
		populateView();
	}

	public void populateView() {		
		
		TableLayout tableLayout = (TableLayout) findViewById(R.id.transactionTypes);
		tableLayout.removeAllViews();

		List<TransactionType> transactionTypes = getTransactionTypeDataSource()
				.findAll();
		for (TransactionType transactionType : transactionTypes) {
			TableRow tableRow = new TableRow(this);
			tableLayout.addView(tableRow);

			TextView id = new TextView(this);
			id.setText(transactionType.getId() + "");
			tableRow.addView(id);

			TextView value = new TextView(this);
			id.setText(transactionType.getType() + "");
			tableRow.addView(value);
			
			Button button = new Button(this);
			button.setOnClickListener(new DeleteListener(this,transactionType,transactionTypeDataSource));
		}
	}
	
	@Override
	protected void onResume() {
		populateView();
		super.onResume();
	}

	public void add(View view){
		TransactionType transactionType = new TransactionType();
		
		EditText editText = ((EditText)findViewById(R.id.transactionType));
		transactionType.setType(editText.getText().toString());
		
		try{
		getTransactionTypeDataSource().insert(transactionType);
		}catch(Exception e){
			Toast.makeText(this, "Insert failed", Toast.LENGTH_SHORT).show();
		}
		populateView();
		
		editText.setText("");
		Toast.makeText(this, "Insert success", Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.manage_transaction_types, menu);
		return true;
	}

	public TransactionTypeDataSource getTransactionTypeDataSource() {
		if (transactionTypeDataSource == null) {
			transactionTypeDataSource = new TransactionTypeDataSource(this);
			transactionTypeDataSource.open();
		}
		return transactionTypeDataSource;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (getTransactionTypeDataSource() != null) {
			getTransactionTypeDataSource().close();
		}
	}

	class DeleteListener implements  OnClickListener{

		Context context;
		TransactionTypeDataSource transactionTypeDataSource;
		TransactionType transactionType;
		public DeleteListener(Context context, TransactionType transactionType, TransactionTypeDataSource transactionTypeDataSource){
			this.context = context;
			this.transactionType = transactionType;
			this.transactionTypeDataSource = transactionTypeDataSource;
		}
		
		@Override
		public void onClick(View v) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

			// Setting Dialog Title
			alertDialog.setTitle("Warning !");

			// Setting Dialog Message
			alertDialog.setMessage("Are you sure you want to delete record: "
					+ transactionType);

			// On pressing Settings button
			alertDialog.setPositiveButton("ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							transactionTypeDataSource.delete(transactionType);
							ManageTransactionTypes.this.populateView();
						}
					});

			// on pressing cancel button
			alertDialog.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});

			// Showing Alert Message
			alertDialog.show();
			
		}
		
	}
}
